//
//  RootViewController.m
//  Demo1013TableViewSQLite
//
//  Created by yang on 10-10-12.
//  Copyright 2010 www.1000phone.com. All rights reserved.
//

#import "RootViewController.h"


@implementation RootViewController
@synthesize name, t2, age;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];
	self.title = @"主页";
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
 // Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations.
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
 */


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
	self.name = nil;
	self.t2 = nil;
	self.age = nil;
    [super dealloc];
}

- (NSString *) databasePath
{
	NSArray *path = NSSearchPathForDirectoriesInDomains(
						NSDocumentDirectory, 
						NSUserDomainMask, 
						YES);
	NSString *pathname = [path objectAtIndex:0];
	NSLog(@"path name is %@", pathname);
	/* 上面得到了 
	 NSString *path = [NSHomeDirectory() 
		stringByAppendingPathComponent:@”Documents"];
	 得到 应用主目录下面的 Documents目录
	 / 就是一个 PathComponent
	 */
	return [pathname 
		stringByAppendingPathComponent:@"mydata.sqlite3"];
}
- (IBAction) insertData:(id)sender
{
	NSString *name2 = self.name.text;
	NSString *gender = self.t2.text;
	//int age = [self.age.text intValue];
	NSString *age2 = self.age.text;
	/* 下面要插入到数据库中了 */
	/* 1. 创建数据库 如果数据库不存在就创建 */
	if (sqlite3_open([[self databasePath] UTF8String],
					 &database) != SQLITE_OK)
	{
		sqlite3_close(database);
		return;
	}
	char *errMsg;
	/* 创建表 表名字 我们就叫 MyTableUserInfo */
	NSString *createSQL = [NSString stringWithString:
				@"CREATE TABLE IF NOT EXISTS MyTableUserInfo"
			" (ID INTEGER PRIMARY KEY, NAME TEXT,  "
			" AGE TEXT, GENDER TEXT)"];
	if (sqlite3_exec(database, [createSQL UTF8String], 
					 NULL, NULL, &errMsg) != SQLITE_OK) {
		printf("erorr %s", errMsg);
		NSLog(@"error");
	}
	
	NSString *insertSQL = [NSString stringWithFormat:
				@"INSERT INTO MyTableUserInfo (NAME, AGE, GENDER)"
				"  VALUES ('%@', '%@', '%@') ",
						   name2, age2, gender];
	if (sqlite3_exec(database, [insertSQL UTF8String],
					 NULL, NULL, NULL) != SQLITE_OK)
	{
		NSLog(@"error for insert");
	}
	self.name.text = @"";
	self.t2.text = @"";
	self.age.text = @"";
}
- (IBAction) showData:(id)sender
{
	ShowDataViewController *vc = [[ShowDataViewController alloc] 
						initWithNibName:@"ShowDataViewController"
								  bundle:nil];
	[self.navigationController pushViewController:vc
						animated:YES];
	[vc release];
}


@end

